#include "input.h"
#include "globals.h"
#include "functions.h"

Input::Input()
{

}

bool Input::isKeyDown(SDLKey key)
{
    return keyStates[key];
}

void Input::handle_input()
{
    while (SDL_PollEvent(&event))
    {
        if (event.type == SDL_QUIT)
        {
            loop = false;
        }

        if (event.type == SDL_KEYDOWN)
        {
            switch (event.key.keysym.sym)
            {
                case SDLK_ESCAPE:
                    loop = false;
                    break;

                case SDLK_F1:
                    P.set_camera();
                    break;

                case SDLK_F12:
                    debug = !debug;
                    break;

                default:
                    break;
            }
        }

        if (event.type == SDL_MOUSEBUTTONDOWN)
        {
            if (event.button.button == SDL_BUTTON_RIGHT)
            {
                if (pointInBox(mm.get_box(), position(mouseX, mouseY)))
                {
                    P.walk_to(mm.mapToX(mouseX), mm.mapToY(mouseY));
                }
                else
                {
                    Npc* npc = Npc::GetNpcOnPosition(mouseX + cam.x(), mouseY + cam.y());
                    if (npc != NULL)
                    {
                        P.attack(npc);
                        output.write("ATTACK!");
                    }
                    else
                    {
                        P.walk_to(cam.x() + mouseX, cam.y() + mouseY);
                    }
                }
            }

            if (event.button.button == SDL_BUTTON_LEFT)
            {
                if (pointInBox(mm.get_box(), position(mouseX, mouseY)))
                    cam.moveCamera(true);
            }
        }

        if (event.type == SDL_MOUSEBUTTONUP)
        {
            if (event.button.button == SDL_BUTTON_LEFT)
            {
                cam.moveCamera(false);
            }
        }

        if (event.type == SDL_MOUSEMOTION)
        {
            mouseX = event.button.x;
            mouseY = event.button.y;
        }
    }

    keyStates = SDL_GetKeyState(NULL);
}

position Input::mousePosition()
{
    return position(mouseX, mouseY);
}
